<?php

/* Because I've deleted my feeds too many times.
 * http://www.xkcd.com/rss.xml xkcd
 * http://www.questionablecontent.net/QCRSS.xml qc
 * http://news.google.com/news?ned=us&hl=en&output=rss gn
 * http://daringfireball.net/index.xml df
 * http://rss.cnn.com/rss/cnn_latest.rss cnn
 * http://www.boston.com/bigpicture/index.xml bp
 * http://feeds.feedburner.com/37signals/beMH sig
 *
 */


function fetchRss(){


    if( $_GET['method'] == 'clear' ) {
        $blank = array();
        update_option('spriggan-rss', $blank);
    }

    if( $_GET['method'] == 'add' ) {
        $feed = array( 'feedUrl' => $_POST['feed-url'], 'shortName' => $_POST['feed-shortName'], 'type' => $_POST['feed-type'] );
        $feeds = get_option('spriggan-rss');
        $feeds[] = $feed;
        update_option('spriggan-rss', $feeds);
    }

    if( $_GET['method'] == 'reset' ) {
        $feeds = array();
	$feeds[] = array( 'feedUrl' => 'http://www.xkcd.com/rss.xml', 'shortName' => 'xkcd', 'type' => 'image');
	$feeds[] = array( 'feedUrl' => 'http://www.questionablecontent.net/QCRSS.xml', 'shortName' => 'qc', 'type' => 'image');
	$feeds[] = array( 'feedUrl' => 'http://news.google.com/news?ned=us&hl=en&output=rss', 'shortName' => 'gn', 'type' => 'news');
	$feeds[] = array( 'feedUrl' => 'http://daringfireball.net/index.xml', 'shortName' => 'df', 'type' => 'normal');
	$feeds[] = array( 'feedUrl' => 'http://rss.cnn.com/rss/cnn_latest.rss', 'shortName' => 'ccn', 'type' => 'news');
	$feeds[] = array( 'feedUrl' => 'http://www.boston.com/bigpicture/index.xml', 'shortName' => 'bp', 'type' => 'image');
	$feeds[] = array( 'feedUrl' => 'http://feeds.feedburner.com/37signals/beMH', 'shortName' => 'sigs', 'type' => 'normal');

	update_option('spriggan-rss', $feeds);
    }

    $paths = explode(PATH_SEPARATOR, get_include_path());
    $path = ABSPATH;
       
    if (array_search($path, $paths) === false){
	array_push($paths, $path);
    }
    set_include_path(implode(PATH_SEPARATOR, $paths));

    require_once 'wp-content/themes/blogtxt/magpierss-0.72/rss_fetch.inc';

    $feeds = get_option('spriggan-rss');
    $html = "";

    foreach($feeds as $feed) {
        $rss = fetch_rss($feed['feedUrl']);
        
        $name = $rss->channel['title'];
        $siteUrl = $rss->channel['link'];
        $shortName = $feed['shortName'];
        $type = $feed['type'];

	$index = 0;
	if($type == 'news'){
	    $index = rand(0, (sizeof($rss->items))-1);
	}

        $desc = $rss->items[$index]['description'];
        if( $desc == '') {
            $desc = $rss->items[$index]['atom_content'];
        }
        $title = $rss->items[$index]['title'];
        $link = $rss->items[$index]['link'];

        $html = $html."<div class='$shortName feed-div'>
                    <h2><a class='feed-name $type-feed-name' href='$siteUrl'>$name</a></h2>
                    <div class='$shortName-title feed-title $type-feed-title' > <a href='$link'><b>$title</b></a></div>
                    <div class='$shortName-content feed-content $type-feed-content' > $desc </div>
                    </div>";
    }

    return $html;
}

?>
